﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

namespace phudieuhungthinh.Helpers
{
    public class UrlRouting
    {
        public static void Initialize()
        {
            RouteTable.Routes.Clear();
            RegisterRoutes(RouteTable.Routes);
        }

        private static void RegisterRoutes(RouteCollection routes)
        {
            // Do not use url routing for requests with .axd/.asmx/.ico extensions
            // This prevents ASP.NET from having to do File.Exists check
            routes.Add("ignoreAXD", new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreASMX", new Route("{resource}.asmx/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreICO", new Route("{resource}.ico/{*pathInfo}", new StopRoutingHandler()));

            // Added to solve a issue with http://(www.)domain.ext/ rendering 404
            routes.Add("Home", new Route("", new StopRoutingHandler()));
            
            // Ignore non-virtual system folders
            routes.Add("ignoreAdmin", new Route("admin/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreAPI", new Route("api/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreLogin", new Route("login/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreUtility", new Route("__utility/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("ignoreFiles", new Route("files/{*pathInfo}", new StopRoutingHandler()));

            routes.Add("articlethumbs", new Route("hinh-anh/tin-tuc/{filename}", new ImageRouteHandler()));
            routes.Add("thumbs", new Route("hinh-anh/phu-dieu/{filename}", new ImageRouteHandler()));
            routes.Add("originalImages", new Route("hinh-anh/dieu-khac/{filename}", new ImageRouteHandler()));
            routes.Add("featureImages", new Route("hinh-anh-noi-bat/dieu-khac/{filename}", new ImageRouteHandler()));

            routes.Add("About", new Route("gioithieu", new CustomRouteHandler("~/Pages/About.aspx")));
            routes.Add("Error404", new Route("loi-404", new CustomRouteHandler("~/errors/404.aspx")));
            routes.Add("Admin", new Route("quantri", new CustomRouteHandler("~/Admin/Default.aspx")));

            routes.Add("Article", new Route("tintuc", new CustomRouteHandler("~/Pages/ArticleCate.aspx")));
            routes.Add("Article1", new Route("tintuc/{url}", new CustomRouteHandler("~/Pages/ArticlePage.aspx")));
            
            routes.Add("Category", new Route("chuyen-muc/{url}", new CustomRouteHandler("~/Pages/CategoryPage.aspx")));
            routes.Add("Category1", new Route("chuyen-muc/{url}/{page}", new CustomRouteHandler("~/Pages/CategoryPage.aspx")));

            routes.Add("Product", new Route("san-pham/{url}", new CustomRouteHandler("~/Pages/ProductPage.aspx")));
            routes.Add("Product1", new Route("san-pham/{url}/{page}", new CustomRouteHandler("~/Pages/ProductPage.aspx")));
        }

        public static void AddRoute(Route route)
        {
            // Ensure route does not already exist
            foreach (Route r in RouteTable.Routes)
            {
                if (r.Url == route.Url)
                    return;
            }

            RouteTable.Routes.Add(route);
        }
    }
}